package cn.doctor.assistant.pad.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.doctor.assistant.pad.R;

/**
 * Created by jiaolf on 2016/3/14.
 */
public class NavigationAdapter extends BaseAdapter {

    private List<MainItem> mainItems;
    private int mLayoutResourceId;
    private Context mContext;

    public NavigationAdapter(Context context, int resId, List<MainItem> data) {
        mContext = context;
        mainItems = data;
        mLayoutResourceId = resId;
    }

    @Override
    public int getCount() {
        return mainItems.size();
    }

    @Override
    public MainItem getItem(int position) {
        return mainItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(mLayoutResourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.main_item_icon);
            viewHolder.label = (TextView) convertView.findViewById(R.id.main_item_lable);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.icon.setImageResource(getItem(position).getImgId());
        viewHolder.label.setText(getItem(position).getLabel());

        return convertView;
    }

    private class ViewHolder{
        private ImageView icon;
        private TextView label;
    }
}
